package com.yang.dao.impl;

import com.yang.dao.RegisterDao;
import com.yang.entity.UserEntity;
import com.yang.entity.VerificationCodeEntity;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Transactional
@Repository
public class RegisterDaoImpl implements RegisterDao {

    private static Logger logger = Logger.getLogger(RegisterDaoImpl.class);

    @Autowired
    SessionFactory sessionFactory;

    @Autowired
    HibernateTemplate hibernateTemplate;

    @Override
    public boolean deleteCode(String QQEmil) {
        Criteria criteria = sessionFactory.openSession().createCriteria(VerificationCodeEntity.class);
        criteria.add(Restrictions.eq("emil",QQEmil));
        List<VerificationCodeEntity> verificationCodeEntityList = criteria.list();
        try{
            for (VerificationCodeEntity verification : verificationCodeEntityList){
                hibernateTemplate.delete(verification);
            }
        }catch (DataAccessException e){
            logger.info("验证码删除失败");
            return false;
        }
        return true;
    }

    @Override
    public boolean saveCode(VerificationCodeEntity verificationCode) {

        try{
            hibernateTemplate.save(verificationCode);
        }catch (DataAccessException e){
            e.printStackTrace();
            logger.error("验证码保存失败");
            return false;
        }
        return true;
    }

    @Override
    public boolean judgeEmilExist(String QQEmil) {
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(UserEntity.class);
        criteria.add(Restrictions.eq("num",QQEmil));
        UserEntity userEntity = null;
        try{
            userEntity = (UserEntity) criteria.uniqueResult();
        }catch (HibernateException e){
            e.printStackTrace();
            logger.info("查找出错");
            session.close();
            return false;
        }
        if(userEntity != null){
            logger.info("存在邮箱");
            session.close();
            logger.info("userEntity = "+userEntity.toString());
            return false;
        }
        session.close();
        return true;
    }

    @Override
    public VerificationCodeEntity findVerificationCode(String QQEmil) {
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(VerificationCodeEntity.class);
        criteria.add(Restrictions.eq("emil",QQEmil));
        VerificationCodeEntity verificationCodeEntity = (VerificationCodeEntity) criteria.uniqueResult();
        session.close();
        return verificationCodeEntity;
    }

    @Override
    public boolean saveUser(UserEntity userEntity) {
        try{
            hibernateTemplate.save(userEntity);
        }catch (DataAccessException e){
            e.printStackTrace();
            logger.error("用户保存失败"+e.getMessage());
            return false;
        }
        return true;
    }
}
